// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PulsedInfo.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework
{
    #region Usings

    using System;
    using System.Collections.Generic;

    using Org.OpenTrader.Framework.Interfaces;

    #endregion

    /// <summary>
    /// The pulsed info.
    /// </summary>
    public class PulsedInfo
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="PulsedInfo"/> class.
        /// </summary>
        /// <param name="who">
        /// The who.
        /// </param>
        public PulsedInfo(IPulsable who)
        {
            this.Target = who;
            this.LastPulse = Timings.Now;
            this.NextPulse = this.LastPulse + this.Target.Period;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Last time it was refreshed.
        /// </summary>
        public DateTime LastPulse { get; set; }

        /// <summary>
        /// Next Schedule.
        /// </summary>
        public DateTime NextPulse { get; set; }

        /// <summary>
        /// Who will be executed.
        /// </summary>
        public IPulsable Target { get; set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// The equals.
        /// </summary>
        /// <param name="obj">
        /// The obj.
        /// </param>
        /// <returns>
        /// The equals.
        /// </returns>
        public override bool Equals(object obj)
        {
            var pobj = obj as PulsedInfo;
            if (pobj == null)
            {
                return false;
            }

            return this.Target.Equals(pobj.Target);
        }

        /// <summary>
        /// The get hash code.
        /// </summary>
        /// <returns>
        /// The get hash code.
        /// </returns>
        public override int GetHashCode()
        {
            return this.Target.GetHashCode();
        }

        #endregion

        /// <summary>
        /// How to sort a PulsedInfo (sorted by nearer time to pulse).
        /// </summary>
        public class Comparer : IComparer<PulsedInfo>
        {
            #region Implemented Interfaces

            #region IComparer<PulsedInfo>

            /// <summary>
            /// The compare.
            /// </summary>
            /// <param name="x">
            /// The x.
            /// </param>
            /// <param name="y">
            /// The y.
            /// </param>
            /// <returns>
            /// The compare.
            /// </returns>
            public int Compare(PulsedInfo x, PulsedInfo y)
            {
                var t = (x.NextPulse - y.NextPulse).Milliseconds;
                if (t == 0)
                {
                    // if theoe two datetime are equal (or less than 1millisecond distant), use haschode to sort
                    t = x.Target.GetHashCode() - y.Target.GetHashCode();
                }

                return t;
            }

            #endregion

            #endregion
        }
    }
}